﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class Admin_answer : System.Web.UI.Page
{
    myDataSetTableAdapters.QuestionsTableAdapter quesAdapter = new myDataSetTableAdapters.QuestionsTableAdapter();
    myDataSetTableAdapters.QuestionTypeTableAdapter quesTypeAdapter = new myDataSetTableAdapters.QuestionTypeTableAdapter();
    myDataSetTableAdapters.AnswersTableAdapter answerAdapter = new myDataSetTableAdapters.AnswersTableAdapter();
    myDataSetTableAdapters.Survey_QuestionTableAdapter surQuesAdapter = new myDataSetTableAdapters.Survey_QuestionTableAdapter();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Panel2.Visible = true;
            Panel1.Visible = false;
            Panel3.Visible = false;
            Panel4.Visible = false;
            cmdChooseQues.Visible = false;
            lblInfo.Visible = true;
            displayQues();
            myDataSet.QuestionTypeDataTable quesTypeTable = quesTypeAdapter.GetData();
            DropDownList1.AppendDataBoundItems = true;
            DropDownList1.DataSource = quesTypeTable.DefaultView;
            DropDownList1.DataTextField = "type";
            DropDownList1.DataValueField = "id";
            DropDownList1.DataBind();
        }
    }

    protected void displayQues()
    {
        myDataSet.QuestionsDataTable quesTable = quesAdapter.GetDataConvert();
        GridView1.DataSource = quesTable;
        GridView1.DataBind();
    }

    protected void displayAnswer(int id)
    {
        myDataSet.AnswersDataTable answerTable = answerAdapter.GetDataByQuesId(id);
        GridView2.DataSource = answerTable;
        GridView2.DataBind();
    }

    protected void clearQues()
    {
        txtId.Text = "";
        txtQues.Text = "";
        DropDownList1.SelectedIndex = -1;
    }

    protected void clearAnswer()
    {
        txtAnswerId.Text = "";
        txtAnswer.Text = "";
        lblAnswerError.Visible = false;
    }

    protected bool checkAnswerEmpty()
    {
        bool status = true;
        if (txtAnswer.Text.Trim() == "")
        {
            lblAnswerError.Visible = true;
            status = false;
        }
        else
        {
            lblAnswerError.Visible = false;
        }
        return status;
    }

    protected void cmdChooseQues_Click(object sender, EventArgs e)
    {
        displayQues();
        Panel1.Visible = false;
        Panel2.Visible = true;
        Panel3.Visible = false;
        Panel4.Visible = false;
        cmdChooseQues.Visible = false;
        lblInfo.Visible = true;
        clearQues();
    }

    protected void cmdHide_Click(object sender, EventArgs e)
    {
        displayQues();
        Panel1.Visible = false;
        Panel2.Visible = true;
        Panel3.Visible = false;
        Panel4.Visible = false;
        cmdChooseQues.Visible = false;
        lblInfo.Visible = true;
    }

    protected void getDataQuesById(int id)
    {
        myDataSet.QuestionsDataTable quesTable = quesAdapter.GetDataById(id);
        txtId.Text = quesTable[0]["id"].ToString();
        txtQues.Text = quesTable[0]["question"].ToString();
        DropDownList1.SelectedValue = quesTable[0]["question_type_id"].ToString();
    }

    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        int id = int.Parse(GridView1.DataKeys[e.NewEditIndex].Value.ToString());
        Panel1.Visible = true;
        Panel2.Visible = true;
        Panel3.Visible = false;
        Panel4.Visible = false;
        cmdChooseQues.Visible = false;
        lblInfo.Visible = false;
        clearQues();
        getDataQuesById(id);
    }

    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
        displayQues();
    }


    protected void cmdChoose_Click(object sender, EventArgs e)
    {
        Panel1.Visible = false;
        Panel2.Visible = false;
        Panel3.Visible = true;
        Panel4.Visible = true;
        cmdChooseQues.Visible = true;
        lblQuestionAnswer.Text = txtQues.Text;
        txtQuestionIdAnswer.Text = txtId.Text;
        displayAnswer(int.Parse(txtQuestionIdAnswer.Text.Trim()));
        txtAnswerId.Enabled = false;
        txtAnswerId.BackColor = System.Drawing.Color.WhiteSmoke;
        cmdAddAnswer.Text = "Add";
        clearAnswer();
    }

    protected void cmdAddAnswer_Click(object sender, EventArgs e)
    {
        int questionId = int.Parse(txtQuestionIdAnswer.Text.Trim());
        myDataSet.Survey_QuestionDataTable surQuesTable = surQuesAdapter.GetDataByQuesId(questionId);
        if (surQuesTable.Rows.Count == 0)
        {
            if (checkAnswerEmpty())
            {
                if (cmdAddAnswer.Text == "Add")
                {
                    answerAdapter.InsertQuery(txtAnswer.Text.Trim(), questionId);
                    displayAnswer(questionId);
                }
                else
                {
                    answerAdapter.UpdateQuery(txtAnswer.Text.Trim(), questionId, int.Parse(txtAnswerId.Text.Trim()));
                    displayAnswer(questionId);
                    clearAnswer();
                    cmdAddAnswer.Text = "Add";
                    cmdClearAnswer.Visible = true;
                }
            }
        }
        else
        {
            RegisterStartupScript("as", "<script>alert('Cannot do anything with this question!')</script>");
        }
    }

    protected void cmdClearAnswer_Click(object sender, EventArgs e)
    {
        clearAnswer();
        displayAnswer(int.Parse(txtQuestionIdAnswer.Text.Trim()));
    }

    protected void getDataAnswerById(int id)
    {
        myDataSet.AnswersDataTable answerTable = answerAdapter.GetDataById(id);
        txtAnswerId.Text = answerTable[0]["id"].ToString();
        txtAnswer.Text = answerTable[0]["answer"].ToString();
    }

    protected void GridView2_RowEditing(object sender, GridViewEditEventArgs e)
    {
        int questionId = int.Parse(txtQuestionIdAnswer.Text.Trim());
        myDataSet.Survey_QuestionDataTable surQuesTable = surQuesAdapter.GetDataByQuesId(questionId);
        if (surQuesTable.Rows.Count == 0)
        {
            int id = int.Parse(GridView2.DataKeys[e.NewEditIndex].Value.ToString());
            Panel1.Visible = false;
            Panel2.Visible = false;
            Panel3.Visible = true;
            Panel4.Visible = true;
            cmdChooseQues.Visible = true;
            cmdAddAnswer.Text = "Save";
            cmdClearAnswer.Visible = false;
            clearAnswer();
            getDataAnswerById(id);
        }
        else
        {
            RegisterStartupScript("as", "<script>alert('Cannot do anything with this question!')</script>");
        }
    }

    protected void GridView2_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        int id = int.Parse(GridView2.DataKeys[e.RowIndex].Value.ToString());
        answerAdapter.DeleteQuery(id);
        clearAnswer();
        displayAnswer(int.Parse(txtQuestionIdAnswer.Text.Trim()));
    }

    protected void GridView2_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView2.PageIndex = e.NewPageIndex;
        displayAnswer(int.Parse(txtQuestionIdAnswer.Text.Trim()));
    }
}
